import django.utils.timezone as timezone

from django.db import models

# Create your models here.

from users.models import User
from vne_erp.utils.base_model import BaseModel


class StoreManagement(BaseModel):

    # staff = models.ForeignKey(User, on_delete=models.CASCADE,null=True, verbose_name='店长')
    store = models.CharField(max_length=30, verbose_name='店铺名')
    store_type = models.CharField(max_length=32, default='', verbose_name='店铺类型')
    store_ip = models.CharField(max_length=20, default='', verbose_name='店铺ip地址')
    seller_id = models.CharField(max_length=20, default='', verbose_name='卖家id')
    mws_token = models.CharField(max_length=50, default='', verbose_name='授权token')
    order_update_time = models.DateTimeField(default=timezone.now, verbose_name='订单更新的时间记录')  # 1


    class Meta:
        db_table = 'tb_store'
        verbose_name = '店铺管理'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s: %s' % (self.id, self.store)

class UserAndStore(models.Model):
    staff = models.ForeignKey(User, on_delete=models.CASCADE, null=True, verbose_name='店长')
    store = models.ForeignKey(StoreManagement, on_delete=models.CASCADE, null=True, verbose_name='店铺')

    class Meta:
        unique_together = ('staff', 'store')
        db_table = 'tb_user_store'
        verbose_name = '员工店铺多对多关联'
        verbose_name_plural = verbose_name

# class Flow(BaseModel):
#     flow = models.IntegerField(default=0, verbose_name="流量")


class StoreFinance(models.Model):
    report_id = models.CharField(max_length=32, default='', unique=True, verbose_name='绩效表')
    store = models.ForeignKey(StoreManagement, on_delete=models.CASCADE,related_name='PerformanceReport', null=True, verbose_name='店铺')
    class Meta:
        db_table = 'jx_store_finance'
        verbose_name = '店铺绩效'
        verbose_name_plural = verbose_name

class PerformanceChecklistStatus(models.Model):
    status = models.CharField(max_length=32, verbose_name='状态')
    class Meta:
        db_table = 'jx_PerformanceChecklistStatus'
        verbose_name = '绩效状态'
        verbose_name_plural = verbose_name

class PerformanceChecklist(models.Model):
    """绩效考核表"""
    orderDefectRate = models.OneToOneField(PerformanceChecklistStatus, related_name='orderDefectRate', on_delete=models.CASCADE, null=True, verbose_name='订单缺陷率')
    orderCancellationRate = models.OneToOneField(PerformanceChecklistStatus, related_name='orderCancellationRate', on_delete=models.CASCADE, null=True, verbose_name='订单取消率')
    lateShipmentRate = models.OneToOneField(PerformanceChecklistStatus, related_name='lateShipmentRate', on_delete=models.CASCADE, null=True, verbose_name='延迟交货率')
    policyViolation = models.OneToOneField(PerformanceChecklistStatus, related_name='policyViolation', on_delete=models.CASCADE, null=True, verbose_name='违反政策率')
    onTimeDelivery = models.OneToOneField(PerformanceChecklistStatus, related_name='onTimeDelivery', on_delete=models.CASCADE, null=True, verbose_name='准时交货率')
    validTrackingRate = models.OneToOneField(PerformanceChecklistStatus, related_name='validTrackingRate', on_delete=models.CASCADE, null=True, verbose_name='有效跟踪率')
    contactResponseTime = models.OneToOneField(PerformanceChecklistStatus, related_name='contactResponseTime', on_delete=models.CASCADE, null=True, verbose_name='联系人响应时间')
    returnDissatisfactionRate = models.OneToOneField(PerformanceChecklistStatus, related_name='returnDissatisfactionRate', on_delete=models.CASCADE, null=True, verbose_name='退货不满意率')
    customerServiceDissatisfactionRate = models.OneToOneField(PerformanceChecklistStatus, related_name='customerServiceDissatisfactionRate', on_delete=models.CASCADE, null=True, verbose_name='客户服务满意度')
    productAuthenticityStatus = models.OneToOneField(PerformanceChecklistStatus, related_name='productAuthenticityStatus', on_delete=models.CASCADE, null=True, verbose_name='产品身份验证状态')
    productSafetyStatus = models.OneToOneField(PerformanceChecklistStatus, related_name='productSafetyStatus', on_delete=models.CASCADE, null=True, verbose_name='产品安全状态')
    listingPolicyStatus = models.OneToOneField(PerformanceChecklistStatus, related_name='listingPolicyStatus', on_delete=models.CASCADE, null=True, verbose_name='列表策略状态')
    intellectualPropertyStatus = models.OneToOneField(PerformanceChecklistStatus, related_name='intellectualPropertyStatus', on_delete=models.CASCADE, null=True, verbose_name='智能属性状态')
    store_finance = models.OneToOneField(StoreFinance, on_delete=models.CASCADE,related_name='performanceChecklist', null=True, verbose_name='店铺')
    class Meta:
        db_table = 'jx_PerformanceChecklist'
        verbose_name = '绩效状态列表'
        verbose_name_plural = verbose_name

class TimeFrame(models.Model):
    """时间外键"""
    start = models.DateTimeField(default=timezone.now, verbose_name='时间范围第一天')
    end = models.DateTimeField(default=timezone.now, verbose_name='时间范围最后一天')
    class Meta:
        db_table = 'jx_TimeFrame'
        verbose_name = '绩效时间表'
        verbose_name_plural = verbose_name

class RateAndCount(models.Model):
    rate = models.CharField(max_length=16, verbose_name='百分比')
    count = models.CharField(max_length=16, verbose_name='订单数量')

    class Meta:
        db_table = 'jx_RateAndCount'
        verbose_name = '绩效状态和数量表'
        verbose_name_plural = verbose_name
class OrderDefects(models.Model):
    """订单缺陷总表"""
    store_finance = models.ForeignKey(StoreFinance, on_delete=models.CASCADE,related_name='orderDefects', null=True, verbose_name='店铺')
    class Meta:
        db_table = 'jx_OrderDefects'
        verbose_name = '绩效订单缺陷总表'
        verbose_name_plural = verbose_name

class OrderDefectMetrics(models.Model):
    """订单缺陷列表"""
    orderDefects = models.ForeignKey(OrderDefects, on_delete=models.CASCADE, related_name='orderDefectMetrics', null=True, verbose_name='订单缺陷')
    timeFrame = models.OneToOneField(TimeFrame, on_delete=models.CASCADE, null=True, verbose_name='时间范围')
    orderCount = models.CharField(max_length=16, verbose_name='订单数量')
    orderWithDefects = models.OneToOneField(RateAndCount, related_name='orderWithDefects', on_delete=models.CASCADE, null=True, verbose_name='有缺陷的订单')
    negativeFeedbacks = models.OneToOneField(RateAndCount, related_name='negativeFeedbacks', on_delete=models.CASCADE, null=True, verbose_name='负面反馈')
    a_z_claims = models.OneToOneField(RateAndCount, related_name='a_z_claims', on_delete=models.CASCADE, null=True, verbose_name='索赔')
    chargebacks = models.OneToOneField(RateAndCount, related_name='chargebacks', on_delete=models.CASCADE, null=True, verbose_name='扣款')
    class Meta:
        db_table = 'jx_OrderDefectMetrics'
        verbose_name = '绩效订单缺陷列表'
        verbose_name_plural = verbose_name

class CustomerExperience(models.Model):
    """客户体验总表"""
    store_finance = models.ForeignKey(StoreFinance, on_delete=models.CASCADE,related_name='customerExperience', null=True, verbose_name='店铺')
    class Meta:
        db_table = 'jx_CustomerExperience'
        verbose_name = '绩效客户体验总表'
        verbose_name_plural = verbose_name

class CustomerExperienceMetrics(models.Model):
    """客户体验列表"""
    customerExperience = models.ForeignKey(CustomerExperience, on_delete=models.CASCADE,related_name='customerExperienceMetrics', null=True, verbose_name='客户体验')
    timeFrame = models.OneToOneField(TimeFrame, on_delete=models.CASCADE, null=True, verbose_name='时间范围')
    orderCount = models.CharField(max_length=16, verbose_name='订单数量')
    lateShipment = models.OneToOneField(RateAndCount, related_name='lateShipment', on_delete=models.CASCADE, null=True, verbose_name='再装运')
    preFulfillmentCancellation = models.OneToOneField(RateAndCount, related_name='preFulfillmentCancellation', on_delete=models.CASCADE, null=True, verbose_name='预履行取消')
    refunds = models.OneToOneField(RateAndCount, related_name='refunds', on_delete=models.CASCADE, null=True, verbose_name='退款')
    class Meta:
        db_table = 'jx_CustomerExperienceMetrics'
        verbose_name = '绩效客户体验列表'
        verbose_name_plural = verbose_name

class TrackingInformation(models.Model):
    """跟踪消息总表"""
    store_finance = models.ForeignKey(StoreFinance, on_delete=models.CASCADE,related_name='trackingInformation', null=True, verbose_name='店铺')
    class Meta:
        db_table = 'jx_TrackingInformation'
        verbose_name = '绩效跟踪消息总表'
        verbose_name_plural = verbose_name

class TrackingMetrics(models.Model):
    """跟踪消息列表"""
    trackingInformation = models.ForeignKey(TrackingInformation, on_delete=models.CASCADE, related_name='trackingMetrics', null=True, verbose_name='跟踪消息列表')
    timeFrame = models.OneToOneField(TimeFrame, on_delete=models.CASCADE, null=True, verbose_name='时间范围')
    requiredCategories = models.CharField(max_length=128, null=True, verbose_name='所需类别')
    validTracking = models.OneToOneField(RateAndCount, related_name='validTracking', on_delete=models.CASCADE, null=True, verbose_name='有效跟踪')
    onTimeDelivery = models.OneToOneField(RateAndCount, related_name='onTimeDelivery', on_delete=models.CASCADE, null=True, verbose_name='按时送达')
    class Meta:
        db_table = 'jx_TrackingMetrics'
        verbose_name = '绩效跟踪消息列表'
        verbose_name_plural = verbose_name

class BuyerSellerContactResponseTimeMetrics(models.Model):
    """卖方买方联络返回时间度量总表"""
    store_finance = models.ForeignKey(StoreFinance, on_delete=models.CASCADE,related_name='buyerSellerContactResponseTimeMetrics', null=True, verbose_name='店铺')
    class Meta:
        db_table = 'jx_BuyerSellerContactResponseTimeMetrics'
        verbose_name = '绩效卖方买方联络返回时间度量总表'
        verbose_name_plural = verbose_name

class ResponseTimeMetrics(models.Model):
    """卖方买方联络返回时间度量列表"""
    responseTimeMetrics = models.ForeignKey(BuyerSellerContactResponseTimeMetrics, on_delete=models.CASCADE, related_name='responseTimeMetrics',  null=True, verbose_name='卖方买方联络返回时间度量列表')
    timeFrame = models.OneToOneField(TimeFrame, on_delete=models.CASCADE, null=True, verbose_name='时间范围')
    averageResponseTimeInHours = models.CharField(max_length=16, verbose_name='小时内平均响应时间')
    responseUnder24Hours = models.CharField(max_length=16, verbose_name='24小时内响应')
    responseTimeGreaterThan24Hours = models.CharField(max_length=16, verbose_name='响应时间大于24小时')
    noResponseForContactsOlderThan24Hours = models.CharField(max_length=16, verbose_name='超过24小时没有任何联系没有')
    class Meta:
        db_table = 'jx_ResponseTimeMetrics'
        verbose_name = '绩效卖方买方联络返回时间度量列表'
        verbose_name_plural = verbose_name

class CustomerService(models.Model):
    """顾客服务总表"""
    store_finance = models.ForeignKey(StoreFinance, on_delete=models.CASCADE,related_name='customerService', null=True, verbose_name='店铺')
    class Meta:
        db_table = 'jx_CustomerService'
        verbose_name = '绩效顾客服务总表'
        verbose_name_plural = verbose_name

class CustomerServiceMetrics(models.Model):
    """顾客服务列表"""
    customerServiceMetrics = models.ForeignKey(CustomerService, on_delete=models.CASCADE, null=True, related_name='customerServiceMetrics',  verbose_name='顾客服务')
    timeFrame = models.OneToOneField(TimeFrame, on_delete=models.CASCADE, null=True, verbose_name='时间范围')
    customerServiceDissatisfaction = models.OneToOneField(RateAndCount, on_delete=models.CASCADE, null=True, verbose_name='顾客服务满意度')
    class Meta:
        db_table = 'jx_CustomerServiceMetrics'
        verbose_name = '绩效顾客服务列表'
        verbose_name_plural = verbose_name

class ReturnDissatisfaction(models.Model):
    """返回满意度总表"""
    store_finance = models.ForeignKey(StoreFinance, on_delete=models.CASCADE,related_name='returnDissatisfaction', null=True, verbose_name='店铺')
    class Meta:
        db_table = 'jx_ReturnDissatisfaction'
        verbose_name = '绩效返回满意度总表'
        verbose_name_plural = verbose_name

class ReturnDissatisfactionMetrics(models.Model):
    """返回满意度列表"""
    returnDissatisfactionMetrics = models.ForeignKey(ReturnDissatisfaction, on_delete=models.CASCADE, related_name='returnDissatisfactionMetrics',  null=True, verbose_name='返回满意度')
    timeFrame = models.OneToOneField(TimeFrame, on_delete=models.CASCADE, null=True, verbose_name='时间范围')
    returnOrderCount = models.CharField(max_length=16, verbose_name='退货订单数量')
    returnDissatisfaction = models.OneToOneField(RateAndCount, related_name='returnDissatisfaction', on_delete=models.CASCADE, null=True, verbose_name='不满退款')
    negativeReturnFeedback = models.OneToOneField(RateAndCount, related_name='negativeReturnFeedback', on_delete=models.CASCADE, null=True, verbose_name='返回负反馈')
    lateResponse = models.OneToOneField(RateAndCount, related_name='lateResponse', on_delete=models.CASCADE, null=True, verbose_name='迟回应')
    invalidRejection = models.OneToOneField(RateAndCount, related_name='invalidRejection', on_delete=models.CASCADE, null=True, verbose_name='无效回拒')
    class Meta:
        db_table = 'jx_ReturnDissatisfactionMetrics'
        verbose_name = '绩效返回满意度列表'
        verbose_name_plural = verbose_name

class ProductAuthenticityData(models.Model):
    """产品验证数据"""
    timeFrame = models.OneToOneField(TimeFrame, on_delete=models.CASCADE, null=True, verbose_name='时间范围')
    defectCount = models.CharField(max_length=16, verbose_name='缺陷计数')
    store_finance = models.OneToOneField(StoreFinance, on_delete=models.CASCADE,related_name='productAuthenticityData', null=True, verbose_name='店铺')
    class Meta:
        db_table = 'jx_ProductAuthenticityData'
        verbose_name = '绩效产品验证数据'
        verbose_name_plural = verbose_name

class ProductSafetyData(models.Model):
    """产品安全数据"""
    timeFrame = models.OneToOneField(TimeFrame, on_delete=models.CASCADE, null=True, verbose_name='时间范围')
    defectCount = models.CharField(max_length=16, verbose_name='缺陷计数')
    store_finance = models.OneToOneField(StoreFinance, on_delete=models.CASCADE,related_name='productSafetyData', null=True, verbose_name='店铺')

    class Meta:
        db_table = 'jx_ProductSafetyData'
        verbose_name = '绩效产品安全数据'
        verbose_name_plural = verbose_name

class ListingPolicyData(models.Model):
    """列表策略数据"""
    timeFrame = models.OneToOneField(TimeFrame, on_delete=models.CASCADE, null=True, verbose_name='时间范围')
    defectCount = models.CharField(max_length=16, verbose_name='缺陷计数')
    store_finance = models.OneToOneField(StoreFinance, on_delete=models.CASCADE,related_name='listingPolicyData',  null=True, verbose_name='店铺')
    class Meta:
        db_table = 'jx_ListingPolicyData'
        verbose_name = '绩效列表策略数据'
        verbose_name_plural = verbose_name

class IntellectualPropertyData(models.Model):
    """智能属性数据"""
    timeFrame = models.OneToOneField(TimeFrame, on_delete=models.CASCADE, null=True, verbose_name='时间范围')
    defectCount = models.CharField(max_length=16, verbose_name='缺陷计数')
    store_finance = models.OneToOneField(StoreFinance, on_delete=models.CASCADE,related_name='intellectualPropertyData',  null=True, verbose_name='店铺')
    class Meta:
        db_table = 'jx_IntellectualPropertyData'
        verbose_name = '绩效智能属性数据'
        verbose_name_plural = verbose_name